//
//  CalculateView.m
//  SZBank
//
//  Created by rimi on 16/7/25.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "CalculateView.h"
#import "CalculateTableViewCell.h"
@interface CalculateView()<UITableViewDataSource>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
}

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIScrollView *scrollView;
@end
@implementation CalculateView

- (instancetype)initWithFrame:(CGRect)frame withIndex:(NSInteger)index withArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = index;
        dataArray = array;
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        self.backgroundColor = [UIColor clearColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.57, frameHeight * 0.02, frameWidth * 0.33, frameHeight * 0.08)];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.text = @"以下结果仅供参考";
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(frameWidth * 0.9, 0, frameWidth * 0.07, frameHeight * 0.1)];
        imageView.image = [LocalDataManager getImageWithSourceOfPath:@"放大镜"];
        [self addSubview:label];
        [self addSubview:imageView];
        [self addSubview:self.scrollView];
        [self initUserInterface];
    }
    return self;
}

- (void)initUserInterface{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, (self.index + 1) * 30)];
    self.tableView.rowHeight = 30;
    self.tableView.dataSource = self;
    self.tableView.layer.borderWidth = 2;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CalculateTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.scrollView addSubview:self.tableView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CalculateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *array = @[@"期数",@"月还款额",@"月付息额",@"月还本额",@"贷款余额"];
    if (indexPath.row == 0) {
        /**  空格内容 */
        for (int i = 0; i < 5; i ++) {
            UILabel *titles = [[UILabel alloc] initWithFrame:CGRectMake(0.202 * frameWidth * (i + 0.04), frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
            titles.text = array[i];
            titles.textAlignment = NSTextAlignmentCenter;
            titles.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:titles];
        }
    }
    else{
        CGFloat payMoney = [dataArray[0] integerValue];
        CGFloat month = [dataArray[1] integerValue];
        CGFloat percent = [dataArray[2] floatValue];
        CGFloat money1 = payMoney / month;
        CGFloat money2 = money1 / 100;
        UILabel *numbers = [[UILabel alloc] initWithFrame:CGRectMake(0.2 * frameWidth * 0.4, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
        numbers.text = [NSString stringWithFormat:@"%ld",indexPath.row];
        UILabel *pay1 = [[UILabel alloc] initWithFrame:CGRectMake(0.19 * frameWidth * 1.1, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
        pay1.textAlignment = NSTextAlignmentCenter;
        pay1.text = [NSString stringWithFormat:@"%.3f",money1 + money2];
        UILabel *pay2 = [[UILabel alloc] initWithFrame:CGRectMake(0.19 * frameWidth * 2.2, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
        pay2.textAlignment = NSTextAlignmentCenter;
        pay2.text = [NSString stringWithFormat:@"%.3f",percent - indexPath.row + 1];
        UILabel *pay3 = [[UILabel alloc] initWithFrame:CGRectMake(0.19 * frameWidth * 3.3, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
        pay3.textAlignment = NSTextAlignmentCenter;
        pay3.text = [NSString stringWithFormat:@"%.3f",(money1 - money2) - (indexPath.row) * money2];
        UILabel *pay4 = [[UILabel alloc] initWithFrame:CGRectMake(0.19 * frameWidth * 4.3, frameHeight * 0.01, frameWidth * 0.18, frameHeight * 0.1)];
        pay4.text = [NSString stringWithFormat:@"%.3f",payMoney - indexPath.row * money1];
        pay4.textAlignment = NSTextAlignmentCenter;
        [cell.contentView addSubview:numbers];
        [cell.contentView addSubview:pay1];
        [cell.contentView addSubview:pay2];
        [cell.contentView addSubview:pay3];
        [cell.contentView addSubview:pay4];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.index + 1;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, frameHeight * 0.1, frameWidth, frameHeight)];
        _scrollView.contentSize = CGSizeMake(frameWidth, frameHeight * 2);
        _scrollView.backgroundColor = [UIColor clearColor];
    }
    return _scrollView;
}

@end
